FROM python:3.11-slim

# 设置环境变量
ENV APP_HOME=/opt/idcops \
    PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    DEBIAN_FRONTEND=noninteractive \
    TZ=Asia/Shanghai

# 创建应用目录
WORKDIR $APP_HOME

# 替换系统安装源为中科大
RUN sed -i 's|deb.debian.org|mirrors.ustc.edu.cn|g' /etc/apt/sources.list.d/debian.sources
# snmp-mibs-downloader requires non-free
RUN sed -i 's|Components: main|Components: main contrib non-free|' /etc/apt/sources.list.d/debian.sources


# 安装系统依赖
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        tzdata \
        curl \
        ca-certificates \
        build-essential \
        libffi-dev \
        file \
        python3-dev \
        libjpeg-dev \
        zlib1g-dev \
        libfreetype6-dev \
        liblcms2-dev \
        libopenjp2-7-dev \
        libtiff5-dev \
        tk-dev \
        tcl-dev \
        libharfbuzz-dev \
        libfribidi-dev \
        libjpeg62-turbo-dev \
        g++ \
        netcat-openbsd \
        libpq-dev \
        pkg-config \
        nmap \
        gcc \
        libsnmp-dev \
        snmp-mibs-downloader \
        # pytesseract 依赖
        tesseract-ocr \
        tesseract-ocr-chi-sim \
        tesseract-ocr-chi-tra \
        tesseract-ocr-jpn \
        tesseract-ocr-kor \
    && ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime \
    && echo ${TZ} > /etc/timezone \
    && rm -rf /var/lib/apt/lists/* \
    && apt-get clean

# Ensure the installed binary is on the `PATH`
ENV PATH="${APP_HOME}/.venv/bin/:/root/.local/bin/:$PATH"

# 创建必要目录
RUN mkdir -p ${APP_HOME} \
    && mkdir -p ${APP_HOME}/static \
    && mkdir -p ${APP_HOME}/media \
    && mkdir -p ${APP_HOME}/media/ml/ \
    && mkdir -p ${APP_HOME}/logs \
    && mkdir -p ${APP_HOME}/db-data \
    && mkdir -p ${APP_HOME}/run

WORKDIR ${APP_HOME}

# 复制项目文件
COPY . ${APP_HOME}/

# 复制并设置 entrypoint
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

# 升级 pip 并安装 Python 依赖
RUN pip install -U pip setuptools wheel --no-cache -i https://pypi.tuna.tsinghua.edu.cn/simple --no-cache-dir

# pip 安装 uv
RUN pip install uv -i https://pypi.tuna.tsinghua.edu.cn/simple --no-cache-dir

RUN uv sync --locked --no-cache

# 设置 entrypoint
ENTRYPOINT ["/entrypoint.sh"]
